package com.tedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tedu.pojo.User;
import com.tedu.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	/**1.用户注册*/
	@RequestMapping("/register")
	public String register(User user,Model model) {
		if (userService.isUserExist(user)) {
			model.addAttribute("msg", "用户名已存在！");
		}else {
			userService.addUser(user);
			model.addAttribute("msg", "注册成功！");
			return "index";
		}
		return "yonghe_regist";
	}
	
	/**2.用户登录*/
	@RequestMapping("/login")
	public String login(User user,Model model) {
		if (!userService.isUserExist(user)) {
			model.addAttribute("msg", "用户名不存在！");
		}else {
			if (user.getPassword().equals(userService.findPassword(user))) {
				model.addAttribute("msg", "登录成功！");
				return "index";
			}else {
				model.addAttribute("msg", "密码错误！");
			}
		}
		return "yonghe_login";
	}
	
	/**3.修改密码*/
	@RequestMapping("/updatePassword")
	public String updatePassword(User user,String newPassword,Model model) {
		if (!userService.isUserExist(user)) {
			model.addAttribute("msg", "用户名不存在！");
		}else {
			if (user.getPassword().equals(userService.findPassword(user))) {//密码正确
				userService.updatePassword(user, newPassword);
				model.addAttribute("msg", "密码修改成功！");
			}else {
				model.addAttribute("msg", "密码错误！");
			}
		}
		return "index";
	}
}
